#include<iostream>
#include<cstdio>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

#define SIZE 1024

void* MyMalloc(int size)
{
    //建立映射（匿名映射）--- 可读可写
    void* mmap_addr = mmap(nullptr, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    if (mmap_addr == MAP_FAILED)
    {
        perror("mmap");
        exit(EXIT_FAILURE);
    }
    return mmap_addr;
}

void* MyFree(void* mmap_addr, int size)
{
    if(munmap(mmap_addr, size) == -1)
    {
        perror("munmap");
        exit(EXIT_FAILURE);
    }
}

int main()
{
    char* ptr = (char*)MyMalloc(SIZE);

    //写入
    for (int c = 'a', i = 0; c <= 'z'; c++, i++)
    {
        ptr[i] = c;
    }
    //读取
    std::cout << "写入后的addr content: " << ptr << std::endl;
    MyFree(ptr, SIZE);

    return 0;
}